package com.palm.hubo2o.util;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.palm.hubo2o.R;
import com.palm.hubo2o.model.LazyReqBody;
import com.palm.hubo2o.model.LazyResBody;
import com.palm.hubo2o.network.PalmCallBack;
import com.palm.hubo2o.network.Request;
import com.palm.hubo2o.network.Result;
import com.palm.palmutils.widget.EasyRecyclerView;
import com.palm.palmutils.widget.refreshlayout.RefreshLayout;

import retrofit2.Call;

/**
 * Created by wongqi on 16/6/14.
 */
public abstract class LazyHelper<E extends LazyReqBody, T extends LazyResBody> {


    private RefreshLayout mRefreshLayout;
    private EasyRecyclerView mRecyclerView;
    private Activity mActivity;
    private int mPage;
    private int mTotal;

    private final int perSize = 20;

    public void attach(RefreshLayout refreshLayout, EasyRecyclerView recyclerView, Activity activity) {
        mRefreshLayout = refreshLayout;
        mRecyclerView = recyclerView;
        mActivity = activity;
        mRefreshLayout.setOnRefreshListener(mOnRefreshListener);
        mRecyclerView.addOnScrollListener(mOnScrollListener);
        mRefreshLayout.setHeaderColorSchemeResources(R.color.red, R.color.blue);
        mRefreshLayout.setFooterColorSchemeResources(R.color.yellow, R.color.red, R.color.blue, R.color.green);
    }

    public void detach() {
        mRefreshLayout = null;
        mRecyclerView = null;
        mActivity = null;
    }

    public void loadFirst() {
        mRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mRefreshLayout.setHeaderRefreshing(true);
                mOnRefreshListener.onHeaderRefresh();
            }
        });

    }

    public abstract void onLazyResponse(T lazyResBody, boolean refresh);

    public abstract Call<Result<T>> getLazyCall(Request<E> request);

    public abstract Request<E> getLazyRequest();

    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!mRefreshLayout.isRefreshing() && mRefreshLayout.isAlmostBottom() && mPage * perSize < mTotal) {
                // Get next page
                mRefreshLayout.setFooterRefreshing(true);
                mOnRefreshListener.onFooterRefresh();
            }
        }
    };

    private RefreshLayout.OnRefreshListener mOnRefreshListener = new RefreshLayout.OnRefreshListener() {

        @Override
        public void onHeaderRefresh() {
            Request<E> request = getLazyRequest();
            request.getBody().setSize(perSize);
            request.getBody().setPage(mPage = 1);
            Call<Result<T>> call = getLazyCall(request);
            call.enqueue(new PalmCallBack<T>(mActivity) {
                @Override
                public void succeed(T lazyResBody) {
                    onLazyResponse(lazyResBody, true);
                    mTotal = lazyResBody.getCount();
                    mRefreshLayout.setHeaderRefreshing(false);
                }

                @Override
                public void failed(String message) {
                    mRefreshLayout.setHeaderRefreshing(false);
                    Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onFooterRefresh() {
            if (mPage * perSize >= mTotal) {
                Toast.makeText(mRecyclerView.getContext(), "没有更多了哦", Toast.LENGTH_SHORT).show();
                mRefreshLayout.setFooterRefreshing(false);
            } else {
                Request<E> request = getLazyRequest();
                request.getBody().setSize(perSize);
                request.getBody().setPage(++mPage);
                Call<Result<T>> call = getLazyCall(request);
                call.enqueue(new PalmCallBack<T>(mActivity) {
                    @Override
                    public void succeed(T lazyResBody) {
                        onLazyResponse(lazyResBody, false);
                        mTotal = lazyResBody.getCount();
                        mRefreshLayout.setFooterRefreshing(false);
                    }

                    @Override
                    public void failed(String message) {
                        mRefreshLayout.setFooterRefreshing(false);
                        Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
                    }
                });
            }

        }
    };


}
